import Vue from 'vue'
import accountApi from '@/api/account'
import * as types from '../mutation-types'
import { SYS_FLAG, LOGIN_STATUS } from '../constant'

// initial state
const state = {
  loginStatus: 0,
  userInfo: {}
}

// getters
const getters = {
  loginStatus: state => state.loginStatus,
  userInfo: state => state.userInfo
}

// actions
const actions = {
  async checkLoginStatus ({ commit }) {
    // 使用默认的登录状态，lazyload
    let auth = localStorage.getItem(SYS_FLAG.AUTH)
    if (auth) {
      commit(types.ACCOUNT_ISLOGIN)
      return
    }

    // 手动检查
    try {
      const { body } = await accountApi.checkLogin()
      if (Vue.prototype.$apiHelper.isLogin(body.responseCode)) {
        commit(types.ACCOUNT_ISLOGIN)
        return
      }
      commit(types.ACCOUNT_NOLOGIN)
    } catch (e) {
      commit(types.ACCOUNT_NOLOGIN)
      return
    }
  },
  async login ({ commit }, loginInfo) {
    try {
      const { body } = await accountApi.login(loginInfo)
      if (Vue.prototype.$apiHelper.requestSuccessed(body.responseCode)) {
        if (body.responseData) {
          commit(types.ACCOUNT_LOGIN_SUCCESS, body.responseData.token)
          commit(types.GET_ACCOUNT_USER_INFO, body.responseData)
        } else {
          commit(types.ACCOUNT_LOGIN_FAILED)
        }
      } else {
        commit(types.ACCOUNT_LOGIN_FAILED)
      }
    } catch (e) {
      commit(types.ACCOUNT_LOGIN_ERROR)
    }
  },
  async logout ({ commit }) {
    setTimeout(() => {
      commit(types.ACCOUNT_LOGOUT)
    }, 500)
    await accountApi.logout()
  },
  async getUserInfo ({ commit }) {
    let localStorageUserInfo = localStorage.getItem(SYS_FLAG.USER_INFO)
    if (localStorageUserInfo && localStorageUserInfo.length > 0) {
      let userInfo = JSON.parse(localStorageUserInfo)
      commit(types.GET_ACCOUNT_USER_INFO, userInfo)
      return userInfo
    }
    commit(types.ACCOUNT_LOGOUT)
  },
  async changeMobile ({ commit }, newmobile) {
    commit(types.CHANGE_MOBILE, newmobile)
  }
}

// mutations
const mutations = {
  [types.ACCOUNT_ISLOGIN] (state) {
    state.loginStatus = LOGIN_STATUS.OK
  },

  [types.ACCOUNT_NOLOGIN] (state) {
    state.loginStatus = LOGIN_STATUS.NOLOGIN
  },

  [types.ACCOUNT_LOGIN_SUCCESS] (state, token) {
    localStorage.setItem(SYS_FLAG.AUTH, token)
    state.loginStatus = LOGIN_STATUS.SUCCESS
  },

  [types.ACCOUNT_LOGIN_FAILED] (state) {
    state.loginStatus = LOGIN_STATUS.FAILED
  },

  [types.ACCOUNT_LOGIN_ERROR] (state) {
    state.loginStatus = LOGIN_STATUS.ERROR
  },

  [types.ACCOUNT_LOGOUT] (state) {
    localStorage.clear()
    sessionStorage.clear()
    state.userInfo = {}
    state.loginStatus = LOGIN_STATUS.NOLOGIN
  },

  [types.GET_ACCOUNT_USER_INFO] (state, userInfo) {
    localStorage.setItem(SYS_FLAG.USER_INFO, JSON.stringify(userInfo))
    state.userInfo = userInfo
  },

  [types.CHANGE_MOBILE] (state, newmobile) {
    state.userInfo.mobile = newmobile
  }
}

export default {
  state,
  getters,
  actions,
  mutations
}
